package com.xxrl.hmwork.service;

import com.xxrl.hmwork.commons.StringUtils;
import com.xxrl.hmwork.conf.MyConfig;
import com.xxrl.hmwork.domain.User;
import com.xxrl.hmwork.repository.UserRepository;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mis
 */
@Service
public class UserService {
    Logger logger = LoggerFactory.getLogger(UserService.class);

    private static String[] types = {"admin", "editor"};
    private static String[] introductions = {"I am a super administrator", "I am an editor"};
    private static String[] avatars = {"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
            "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"};

    private final UserRepository userRepository;


    @Autowired
    public UserService(UserRepository userRepository, MyConfig myConfig) {
        this.userRepository = userRepository;
        User user = userRepository.getUserByNameAndPassword(myConfig.getAdminUsername(), myConfig.getAdminPassword());
        if (user == null) {
            user = new User();
            user.setType(0);
            user.setEmail("admin");
            user.setName(myConfig.getAdminUsername());
            user.setPassword(myConfig.getAdminPassword());
            user.setPhone("admin");
            userRepository.save(user);
        }
    }

    /**
     * 用户登陆
     * @param name     账号
     * @param password    密码
     * @return  返回用户对象
     */
    public User login(String name, String password) {
        logger.info(String.format("[Login]: %s", name));
        return userRepository.getUserByNameAndPassword(name,password);
    }

    /**
     * 通过用户类型返回用户的Token标识
     * @param type 0: admin, 1: editor
     * @return token 字符串
     */
    public String getToken(int type) {
        return types[type];
    }

    /**
     * 通过token获得用户的类型
     * @param token 字符串
     * @return 下标
     */
    public int getType(@NotNull String token) {
        switch (token) {
            case "[\"admin\"]":
                return 0;
            case "[\"editor\"]":
                return 1;
            default:
                return -1;
        }
    }

    /**
     * 获得用户描述信息json
     * @param type  用户类型
     * @return  用户描述信息json
     */
    public Map<String, String> getInfo(int type, String name) {
        Map<String, String> map = new HashMap<>(4);
        map.put("roles", types[type]);
        map.put("introduction", introductions[type]);
        map.put("avatar", avatars[type]);
        map.put("name", name);
        return map;
    }

    public List<User> find(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        return userRepository.getUsersByNameLike(name);
    }

    public boolean registration(@NotNull User user) {
        User t = userRepository.getUserByNameAndEmailAndPhone(user.getName(),user.getEmail(),user.getPhone());
        // 如果能查找到说明用户已经存在
        if (null != t) {
            return false;
        }
        user = userRepository.save(user);
        return null != user.getOid();
    }

    public String lookPassword(String name, String email, String phone) {
        List<User> users = userRepository.getUsersByNameLike(name);
        for (User u : users) {
            if(email.equals(u.getEmail()) && phone.equals(u.getPhone())) {
                return u.getPassword();
            }
        }
        return null;
    }

    public Page<User> list(Pageable page) {
        return userRepository.findAll(page);
    }
}
